﻿
Partial Public Class User
    Private mRest As REST
    Public ReadOnly Property REST As REST
        Get
            Return mRest
        End Get
    End Property

    Public Property Application As lfm.Application
    Friend Sub New(ByVal App As lfm.Application, ByVal restClass As lfm.REST)
        Me.Application = App
        Me.mRest = restClass
    End Sub

    ''' <summary>
    ''' Get information about a user profile.
    ''' </summary>
    ''' <param name="username">Username</param>
    ''' <param name="applyTo">Base <seealso cref="lfm.UserInfo">UserInfo</seealso> object to write.</param>
    ''' <returns> An <seealso cref="lfm.UserInfo">UserInfo</seealso> object that contains user profile information.</returns>
    ''' <remarks>This service does not require authentication.</remarks>
    Public Function GetInfo(ByVal username As String, Optional ByVal applyTo As lfm.UserInfo = Nothing) As UserInfo
        Dim result As XDocument = Me.REST.ExecuteRest("user.getInfo", {{"user", username}})
        If applyTo Is Nothing Then applyTo = New UserInfo()

        If result.<lfm>.@status = "error" Then
            Throw New LastFMError(result.<lfm>.Value)
        End If

        With applyTo
            .IsInfoLoaded = True
            .Id = Integer.Parse(result.<lfm>.<user>.<id>.Value)
            .Name = result.<lfm>.<user>.<name>.Value
            .Realname = result.<lfm>.<user>.<realname>.Value
            If Uri.TryCreate(result.<lfm>.<user>.<url>.Value, UriKind.Absolute, .Url) Then : End If
            If Uri.TryCreate(result.<lfm>.<user>.<image>.Value, UriKind.Absolute, .ImageUrl) Then : End If
            .CountryCode = result.<lfm>.<user>.<country>.Value
            .Age = Integer.Parse(result.<lfm>.<user>.<age>.Value)
            If result.<lfm>.<user>.<gender>.Value.ToLowerInvariant = "m" Then .Sex = UserInfo.Gender.Male
            If result.<lfm>.<user>.<subscriber>.Value = "0" Then .Subscriber = False
            .Playcount = Integer.Parse(result.<lfm>.<user>.<playcount>.Value)
            .PlaylistCount = Integer.Parse(result.<lfm>.<user>.<playlists>.Value)
            If result.<lfm>.<user>.<bootstrap>.Value = "0" Then .Bootstrap = False
            .RegisterTime = Date.Parse(result.<lfm>.<user>.<registered>.Value)
        End With

        Return applyTo
    End Function

End Class
